/**
 *
 */
package org.starsource.forum.server.transaction.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Set;

import org.starsource.forum.client.exception.DatabaseException;
import org.starsource.forum.client.exception.PermissionDeniedException;
import org.starsource.forum.client.exception.SessionNotFoundException;
import org.starsource.forum.server.dao.SessionDao;
import org.starsource.forum.server.domain.Category;
import org.starsource.forum.server.domain.Session;
import org.starsource.forum.server.domain.User;
import org.starsource.forum.server.transaction.SessionTransaction;

/**
 * @author james
 *
 */
public class SessionTransactionImpl implements SessionTransaction {

	private static org.apache.log4j.Logger logger = org.apache.log4j.LogManager
	.getLogger(SessionTransactionImpl.class.getName());

	private SessionDao sessionDao;
	public void setSessionDao(SessionDao sessionDao) {
		this.sessionDao = sessionDao;
	}

	/* (non-Javadoc)
	 * @see org.starsource.forum.server.service.SessionService#createSession(org.starsource.forum.server.domain.User, java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public Session	createSession(User requester, String title, Category category) throws DatabaseException {
		Timestamp timestamp = new Timestamp(new Date().getTime()/1000*1000);
		Session session = new Session(requester, title, category, timestamp, timestamp);
		sessionDao.save(session);
		return session;
	}

	/* (non-Javadoc)
	 * @see org.starsource.forum.server.service.SessionService#updateSession(org.starsource.forum.server.domain.User, org.starsource.forum.server.domain.Session)
	 */
	@Override
	public void updateSession(User requester, Session session) throws DatabaseException {
		if (!session.getOwner().equals(requester)) {
			return;
		}
		Timestamp timestamp = new Timestamp(new Date().getTime()/1000*1000);
		session.setUpdateTimestamp(timestamp);
		sessionDao.update(session);
	}

	/* (non-Javadoc)
	 * @see org.starsource.forum.server.service.SessionService#deleteSession(org.starsource.forum.server.domain.User, org.starsource.forum.server.domain.Session)
	 */
	@Override
	public void removeSession(User requester, Session session) throws PermissionDeniedException, DatabaseException {
		logger.warn("用户" + requester.getId() + "正在删除会话" + session.getId());

		if (!session.getOwner().equals(requester)) {
			logger.warn("用户" + requester.getId() + "没有权限删除会话" + session.getId());
			throw new PermissionDeniedException();
		}
		sessionDao.delete(session);
	}

	/* (non-Javadoc)
	 * @see org.starsource.forum.server.service.SessionService#getSession(org.starsource.forum.server.domain.User, int)
	 */
	@Override
	public Session getSession(User requester, int id) throws SessionNotFoundException, DatabaseException {
		Session session = sessionDao.getSession(id);
		return session;
	}

	@Override
	public Set<Session> getSessionSet(User requester) throws DatabaseException {
		Set<Session> sessions = sessionDao.getSessionSet();
		return sessions;
	}

	@Override
	public Set<Session> getSessionSet(User requester, Category category) throws DatabaseException {
		Set<Session> sessions = sessionDao.getSessionSet(category);
		return sessions;
	}

	@Override
	public Set<Session> getSessionSet(User requester, Set<Category> categories)
			throws DatabaseException {
		Set<Session> sessions = sessionDao.getSessionSet(categories);
		return sessions;
	}

	@Override
	public Set<Session> getSessionSetByKeywords(User requester,
			String[] keywords) throws DatabaseException {
		return sessionDao.getSessionSetByKeywords(keywords);
	}

}
